﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Marbles.Core.Content.Pages;

namespace Marbles.Core.Controls
{
    [ToolboxData("<{0}:DynamicContentPanel runat=server></{0}:DynamicContentPanel>")]
    public class DynamicContentPanel : Panel
    {
        public string ContentId { get { object obj = ViewState["ContentId"]; return (obj == null) ? "" : obj.ToString().ToLower(); } set { ViewState["ContentId"] = value.ToLower(); } }

        protected override void CreateChildControls()
        {
            if (string.IsNullOrEmpty(this.ContentId)) { this.ContentId = this.ID.ToLower(); }
            Marbles.Core.Pages.Page p = this.Page as Marbles.Core.Pages.Page;
            PageDefinition def = p.CurrentPage;
            if (def == null) { return; }
            bool isEditor = p.IsEditor || p.IsAdministrator;
            LiteralControl content = new LiteralControl();
            content.ID = "content";
            content.Text = "Content Not Found [" + this.ContentId + "]";

            if (def.Content.ContainsKey(this.ContentId))
            {
                content.Text = def.Content[this.ContentId];
            }

            this.Controls.Add(content);
        }

    }
}
